package com.guaoran.micro.services.spring.cloud.stream.kafka.controller;

import com.guaoran.micro.services.spring.cloud.stream.kafka.stream.producer.MessageProducerBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : gucheng
 * @Description : <br/>
 * @date :2018/12/6 22:54
 */
@RestController
public class KafkaProducerController {
    private final KafkaTemplate<String, String> kafkaTemplate;
    private final MessageProducerBean messageProducerBean;

    private final String topic;
    @Autowired
    public KafkaProducerController(KafkaTemplate<String, String> kafkaTemplate,
                                   @Value("${kafka.topic}") String topic, MessageProducerBean messageProducerBean) {
        this.kafkaTemplate = kafkaTemplate;
        this.topic = topic;
        this.messageProducerBean = messageProducerBean;
    }

    /**
     * 发送消息 api 方式
     * @param message
     * @return
     */
    @GetMapping("/message/send")
    public Boolean sendMessage(@RequestParam String message) {
        kafkaTemplate.send(topic, message);
        return true;
    }

    /**
     * 通过{@link MessageProducerBean} 发送
     * stream 方式发送消息
     * @param message
     * @return
     */
    @GetMapping("/message/send2")
    public Boolean sendMessage2(@RequestParam String message) {
        messageProducerBean.send(message);
        return true;
    }
}
